﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Point2d.App
{
    public class Video
    {
        public Video(Vehicle vehicle)
        {
            _vehicle = vehicle;
        }

        Vehicle _vehicle = null;
        List<Status> _statusList = new List<Status>();
        List<Status> _items = new List<Status>();
        int _index = 0;

        public void Clear()
        {
            _statusList.Clear();
            _items.Clear();
        }

        public void AppendItem(float pos, float angle, float posLat)
        {
            _statusList.Add(new Status(pos, angle, posLat));
        }

        public void Create()
        {
            _index = 0;
            for (int i = 0; i < _statusList.Count - 1; i++)
            {
                Status from = _statusList[i];
                Status to = _statusList[i + 1];

                int count = 100;
                float stepPos = (to._pos - from._pos) / count;
                float stepPosLat = (to._posLat - from._posLat) / count;
                float stepAngle = (to._angle - from._angle) / count;

                _items.Add(new Status(from._pos, from._angle, from._posLat));
                for (int j = 1; j <= count; j++)
                {
                    _items.Add(new Status(from._pos + stepPos * j,
                        from._angle + stepAngle * j,
                        from._posLat + stepPosLat * j));
                }
            }
        }

        public void CreateSubItem(int index)
        {
            _items.Clear();

            _index = 0;
            Status from = _statusList[index];
            Status to = _statusList[index + 1];

            int count = 100;
            float stepPos = (to._pos - from._pos) / count;
            float stepPosLat = (to._posLat - from._posLat) / count;
            float stepAngle = (to._angle - from._angle) / count;

            _items.Add(new Status(from._pos, from._angle, from._posLat));
            for (int j = 1; j <= count; j++)
            {
                _items.Add(new Status(from._pos + stepPos * j,
                    from._angle + stepAngle * j,
                    from._posLat + stepPosLat * j));
            }
        }

        public bool Play()
        {
            if (_index < 0 || _index >= _items.Count)
                return false;

            _vehicle.Set(_items[_index]._pos, _items[_index]._angle, _items[_index]._posLat);
            _vehicle.Update();

            _index++;
            return true;
        }
    }
}
